package com.tools.ad.adscene.inner;


import com.hm.ads.sdk.HmAdsSDK;
import com.hm.adsbuisiness.bean.HmAdPlace;
import com.hm.adsbuisiness.bean.HmAdUnionPlace;
import com.hm.adsbuisiness.bean.banner.AMNRawBannerAd;
import com.hm.adsbuisiness.bean.banner.AMRawBannerAd;
import com.hm.adsbuisiness.bean.interstitial.AMNRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.AMRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.FluctRawIntersitialAd;
import com.hm.adsbuisiness.bean.interstitial.MpRawInterstitialAd;
import com.hm.adsbuisiness.bean.nativead.AmUniRawNativeAd;
import com.hm.adsbuisiness.bean.nativead.RawNativeAd;
import com.hm.adsbuisiness.bean.reward.AMNRawRewardAd;
import com.hm.adsbuisiness.bean.reward.AMRawRewardAd;
import com.hm.adsbuisiness.bean.reward.FluctRawRewardAd;
import com.hm.adsbuisiness.service.AdActionException;
import com.hm.adsbuisiness.utils.AdUtils;
import com.hm.adsui.card.CommonNativeAdCardView;
import com.hm.adsui.window.AmBannerFullScreenWindow;
import com.hm.adsui.window.IFullScreenADListener;
import com.hm.adsui.window.RawNativeFullScreenAdWindow;
import com.hm.base.android.mob.AMApplication;
import com.tools.ad.ADConstant;
import com.tools.ad.AdType;
import com.tools.ad.EntranceType;
import com.tools.ad.R;
import com.tools.ad.adscene.AdReportScene;
import com.tools.ad.strategy.IStrategyExecutor;
import com.tools.ad.util.GARecordUtils;
import com.yanzhenjie.permission.AndPermission;

import static com.tools.ad.AdType.AM_NATIVE;
import static com.tools.ad.AdType.AM_NEW_NATIVE;
import static com.tools.ad.AdType.DA_NATIVE;
import static com.tools.ad.AdType.FB_NATIVE;
import static com.tools.ad.AdType.MP_NATIVE;

/**
 * desc: 普通的插屏广告场景。
 * <b>插件广告加载方式</b></br>
 * <p>
 * <p>从触发开始请求广告，这时弹出过渡动画，广告是否显示，和动画没有直接关系，广告生成广告视图时，直接显示广告</p>
 * <p>
 * <b>支持的广告类型</b></br>
 * <p>插屏(Admob,Mopub)，原生(Admob,Mopub,FB)，BANNER (Admob)</p>
 * date: 2018/4/18
 * author: ancun
 */

public class CommonInterstitialScene extends AdReportScene {
    protected AmBannerFullScreenWindow mAmbBannerFullScreenWindow;
    protected RawNativeFullScreenAdWindow mNativeFullScreenWindow;
    protected HmAdUnionPlace mHmAdPlace;
    protected IStrategyExecutor mExecutor;

    public CommonInterstitialScene(AMApplication context, EntranceType entranceType) {
        super(context, entranceType);
    }

    @Override
    protected CommonNativeAdCardView initCardView() {
        return null;
    }

    @Override
    protected void onAdClose(AdType adType) {
        release();
    }

    @Override
    protected void onADLoaded(AdType adType) {
        //广告成功返回
        showAd();
    }

    @Override
    protected void onADShow(AdType adType) {
        super.onADShow(adType);
        if (mExecutor != null) {
            mExecutor.record();
        }
    }

    @Override
    protected void onADClick(AdType adType) {
        super.onADClick(adType);

        // 非插屏的广告，一般不具有关闭的功能，所以需要在用户点击的时候关闭。
        switch (adType) {
            case DA_NATIVE:
            case FB_NATIVE:
            case AM_NATIVE:
            case MP_NATIVE:
            case AM_NEW_NATIVE:
                if (mNativeFullScreenWindow != null) {
                    mNativeFullScreenWindow.close();
                    mNativeFullScreenWindow = null;
                }
                break;
            case AM_BANNER:
            case AM_NEW_BANNER:
                if (mAmbBannerFullScreenWindow != null) {
                    mAmbBannerFullScreenWindow.close();
                    mAmbBannerFullScreenWindow = null;
                }
                break;
        }
    }

    @Override
    public void release() {
        super.release();

        mAmbBannerFullScreenWindow = null;
        mNativeFullScreenWindow = null;
        mHmAdPlace = null;
    }

    /**
     * 加载广告
     *
     * @param forceLoad 是否忽略配置检查，强制加载广告
     * @return
     */
    @Override
    public boolean load(boolean forceLoad) {
        HmAdUnionPlace adUnionPlace = HmAdsSDK.getInstance().getUnionAd(mEntranceType.getPid());
        if (adUnionPlace == null) {
            return false;
        }
        mHmAdPlace = adUnionPlace;
        mExecutor = getADModule().getADStrategyManager().getExecutorByEntranceType(mEntranceType);
        if (forceLoad || mExecutor.check(adUnionPlace)) {
            return loadUnionPlace(adUnionPlace);
        }

        return false;
    }

    /**
     * 显示广告
     */
    public boolean showAd() {
        boolean result = true;
        if (activeAd instanceof AMRawInterstitialAd) {
            showAmInterstitial();
        } else if (activeAd instanceof AMNRawInterstitialAd) {
            showAmNewInterstitial();
        } else if (activeAd instanceof MpRawInterstitialAd) {
            showMpInterstitial();
        } else if (activeAd instanceof AMRawBannerAd) {
            showAmBannerFullScreen();
        } else if (activeAd instanceof AMNRawBannerAd) {
            showAmNewBannerFullScreen();
        } else if (activeAd instanceof AmUniRawNativeAd) {
            showAmNewNativeFullScreen();
        } else if (activeAd instanceof RawNativeAd) {
            showNativeFullScreen();
        } else if (activeAd instanceof FluctRawIntersitialAd) {
            showFtInterstitial();
        } else if (activeAd instanceof FluctRawRewardAd) {
            showFtRewardAd();
        } else if (activeAd instanceof AMRawRewardAd) {
            showAmRewardAd();
        } else if (activeAd instanceof AMNRawRewardAd) {
            showAmNewRewardAd();
        } else {
            result = false;
        }

        return result;
    }


    /**
     * 内部使用的广告显示回调
     */
    private void onInnerAdShow() {
        if (AdUtils.isFaceBook(activeAd)) {
            onADShow(FB_NATIVE);
        } else if (AdUtils.isAdmob(activeAd)) {
            if ("am".equals(activeAd.getPlatform())) {
                onADShow(AM_NATIVE);
            } else {
                onADShow(AM_NEW_NATIVE);
            }
        } else if (AdUtils.isMopub(activeAd)) {
            onADShow(MP_NATIVE);
        } else {
            onADShow(DA_NATIVE);
        }
    }

    /**
     * 内部使用的广告关闭回调,只有原生广告才会调用。
     */
    private void onInnerAdClose() {
        if (AdUtils.isFaceBook(activeAd)) {
            onAdClose(FB_NATIVE);
        } else if (AdUtils.isAdmob(activeAd)) {
            if ("am".equals(activeAd.getPlatform())) {
                onAdClose(AM_NATIVE);
            } else {
                onAdClose(AM_NEW_NATIVE);
            }
        } else if (AdUtils.isMopub(activeAd)) {
            onAdClose(MP_NATIVE);
        } else {
            onAdClose(DA_NATIVE);
        }

        release();
    }

    /**
     * 显示 BANNER 的全屏
     */
    private void showAmBannerFullScreen() {
        if (mAmbBannerFullScreenWindow != null) {
            mAmbBannerFullScreenWindow = null;
        }

        if (!AndPermission.checkOverlayPermission(imContext)) {
            GARecordUtils.onADShowFailed(
                    imContext, mEntranceType.getName(),
                    AdType.AM_BANNER,
                    ADConstant.ErrorCode.NO_PERMISSION
            );
        }

        try {
            mAmbBannerFullScreenWindow = new AmBannerFullScreenWindow(imContext);
            mAmbBannerFullScreenWindow.isTouchToClose(false);
            mAmbBannerFullScreenWindow.setFullScreenListener(new IFullScreenADListener() {
                @Override
                public void onADShow(HmAdPlace hmAdPlace) {
                    CommonInterstitialScene.this.onADShow(AdType.AM_BANNER);
                }

                @Override
                public void onADClose(HmAdPlace hmAdPlace) {
                    CommonInterstitialScene.this.onAdClose(AdType.AM_BANNER);
                }
            });
            mAmbBannerFullScreenWindow.showAmBanner(mHmAdPlace, (AMRawBannerAd) activeAd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示 BANNER 的全屏
     */
    private void showAmNewBannerFullScreen() {
        if (mAmbBannerFullScreenWindow != null) {
            mAmbBannerFullScreenWindow = null;
        }

        if (!AndPermission.checkOverlayPermission(imContext)) {
            GARecordUtils.onADShowFailed(
                    imContext, mEntranceType.getName(),
                    AdType.AM_NEW_BANNER,
                    ADConstant.ErrorCode.NO_PERMISSION
            );
        }

        try {
            mAmbBannerFullScreenWindow = new AmBannerFullScreenWindow(imContext);
            mAmbBannerFullScreenWindow.isTouchToClose(false);
            mAmbBannerFullScreenWindow.setFullScreenListener(new IFullScreenADListener() {
                @Override
                public void onADShow(HmAdPlace hmAdPlace) {
                    CommonInterstitialScene.this.onADShow(AdType.AM_NEW_BANNER);
                }

                @Override
                public void onADClose(HmAdPlace hmAdPlace) {
                    CommonInterstitialScene.this.onAdClose(AdType.AM_NEW_BANNER);
                }
            });
            mAmbBannerFullScreenWindow.showAmBanner(mHmAdPlace, (AMRawBannerAd) activeAd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示原生的全屏
     */
    private void showNativeFullScreen() {
        if (mNativeFullScreenWindow != null) {
            mNativeFullScreenWindow = null;
        }

        if (!AndPermission.checkOverlayPermission(imContext)) {
            AdType type = DA_NATIVE;
            if (AdUtils.isFaceBook(activeAd)) {
                type = FB_NATIVE;
            } else if (AdUtils.isAdmob(activeAd)) {
                type = AM_NATIVE;
            } else if (AdUtils.isMopub(activeAd)) {
                type = MP_NATIVE;
            }

            GARecordUtils.onADShowFailed(
                    imContext,
                    mEntranceType.getName(),
                    type,
                    ADConstant.ErrorCode.NO_PERMISSION
            );
        }

        mNativeFullScreenWindow = new RawNativeFullScreenAdWindow(imContext);
        mNativeFullScreenWindow.isTouchToClose(false);
        mNativeFullScreenWindow.setFullScreenListener(new IFullScreenADListener() {
            @Override
            public void onADShow(HmAdPlace hmAdPlace) {
                onInnerAdShow();
            }

            @Override
            public void onADClose(HmAdPlace hmAdPlace) {
                onInnerAdClose();
            }

        });
        mNativeFullScreenWindow.showFullScreenNativeAd(mHmAdPlace, (RawNativeAd) activeAd);
    }

    /**
     * 显示原生的全屏
     */
    private void showAmNewNativeFullScreen() {
        if (mNativeFullScreenWindow != null) {
            mNativeFullScreenWindow = null;
        }

        if (!AndPermission.checkOverlayPermission(imContext)) {
            AdType type = DA_NATIVE;
            if (AdUtils.isFaceBook(activeAd)) {
                type = FB_NATIVE;
            } else if (AdUtils.isAdmob(activeAd)) {
                if ("am".equals(activeAd.getPlatform())) {
                    type = AM_NATIVE;
                } else {
                    type = AM_NEW_NATIVE;
                }
            } else if (AdUtils.isMopub(activeAd)) {
                type = MP_NATIVE;
            }

            GARecordUtils.onADShowFailed(
                    imContext,
                    mEntranceType.getName(),
                    type,
                    ADConstant.ErrorCode.NO_PERMISSION
            );
        }

        mNativeFullScreenWindow = new RawNativeFullScreenAdWindow(imContext);
        mNativeFullScreenWindow.isTouchToClose(false);
        mNativeFullScreenWindow.setFullScreenListener(new IFullScreenADListener() {
            @Override
            public void onADShow(HmAdPlace hmAdPlace) {
                onInnerAdShow();
            }

            @Override
            public void onADClose(HmAdPlace hmAdPlace) {
                onInnerAdClose();
            }

        });
        mNativeFullScreenWindow.showFullScreenNativeAd(mHmAdPlace, (AmUniRawNativeAd) activeAd);
    }

    /**
     * Admob 插屏广告
     */
    private void showAmInterstitial() {
        try {
            ((AMRawInterstitialAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    /**
     * Admob NEW 插屏广告
     */
    private void showAmNewInterstitial() {
        try {
            ((AMNRawInterstitialAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    /**
     * Mopub 插屏广告
     */
    private void showMpInterstitial() {
        try {
            ((MpRawInterstitialAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    private void showFtInterstitial() {
        try {
            ((FluctRawIntersitialAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    private void showFtRewardAd() {
        try {
            ((FluctRawRewardAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    private void showAmRewardAd() {
        try {
            ((AMRawRewardAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    private void showAmNewRewardAd() {
        try {
            ((AMNRawRewardAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.full_screen_mp_card_layout;
    }

    @Override
    protected int getVideoLayoutId() {
        return R.layout.full_screen_mp_video_ad_card_layout;
    }
}
